#Include ..\..\_lib\ShinsOverlayClass.ahk

#Include ..\..\_lib\ctrls\Button.ahk

class RegionToolBar {
  w := 182, h := 32

  __New(cb1, cb2) {
    this.overlay := ShinsOverlayClass(0, 0, this.w, this.h, 1, 0, 0,, Meta.name 'ToolGUI')

    c := '0x000000', c2 := '0x003131', c3 := '0x255e90'
    this.box0 := Button(Rect(121, 1, 30, 30), c2, c3)
    this.box1 := Button(Rect(151, 1, 30, 30), c2, c3)
    this.box0.OnEvent(Button.ON_RELEASE, (*) => cb1())
    this.box1.OnEvent(Button.ON_RELEASE, (*) => cb2())
    this.me := MouseEvent(this.overlay.Hwnd)
    this._RegisterEvents()
  }

  Hide() {
    this.overlay.gui.Hide()
  }

  Destroy() {
    this.overlay.gui.Destroy()
  }

  _RegisterEvents() {
    this.me.Register(MouseEvent.Events.LBUTTON_UP, OnLU)
    this.me.Register(MouseEvent.Events.LBUTTON_DOWN, OnLD)

    OnLU(point, *) {
      this.box0.OnRelease(point.InRect(this.box0._rect))
      this.box1.OnRelease(point.InRect(this.box1._rect))
      this.Refresh()
    }

    OnLD(point, *) {
      if point.InRect(this.box0._rect)
        this.box0.OnPressed()
      if point.InRect(this.box1._rect)
        this.box1.OnPressed()
      this.Refresh()
    }
  }

  Adapt(wx, wy, ww, wh) {
    x := ((wx + ww) < this.w) ? 0 : wx + ww - this.w
    y := wy + wh >= (A_ScreenHeight - this.h) ? wy - this.h - 1 : wy + wh + 1
    if wh >= (A_ScreenHeight - this.h)
      y := wy + wh - this.h - 2, x := x - 2
    this.overlay.gui.Move(x, y)
    return this
  }

  SetTip(t) {
    this.tip := t
    return this
  }

  Refresh() {
    overlay := this.overlay

    overlay.BeginDraw()
    drawBase()
    overlay.DrawText(this['tip'], 1, 1, , , , 'w120 h30 aCenter aMid')
    this.box0.Draw(overlay)
    this.box1.Draw(overlay)

    overlay.DrawText('✗', 121, 0, , , , 'w30 h30, aCenter aMid')
    overlay.DrawText('✓', 151, 0, , , , 'w30 h30, aCenter aMid')
    overlay.DrawLine(152, 0, 152, 32, '0xff01ffe1')
    overlay.EndDraw()

    drawBase() {
      overlay.FillRectangle(0, 0, this.w, this.h, '0x88000000')
      overlay.DrawRectangle(1, 1, this.w - 1, this.h - 1, '0xff01ffe1')
      overlay.DrawLine(121, 0, 121, 32, '0xff01ffe1')
    }
  }
}